//
//  YFHomeAllViewController.m
//  YFListenStock
//
//  Created by funplus on 2020/12/14.
//  Copyright © 2020 ChinaRainbow-iOS. All rights reserved.
//

#import "YFHomeAllViewController.h"
#import "YFHomeBannerCell.h"
#import "YFBannerModel.h"
#import "YFBaseWebViewController.h"
#import "YFLoginRegistViewController.h"
#import "YFHomeViewModel.h"
#import "YFHomeTopCategoryCell.h"
#import "YFNewsInfoViewController.h"
#import "YFNewVoice60sCollectionViewCell.h"
#import "ATVideoWindowView.h"
#import "YFVoiceToTextView.h"
#import "YFIntradayAlertsCollectionViewCell.h"
#import "YFAlertsModel.h"
#import "YFHomeChatRoomsCollectionViewCell.h"
#import "YFHomeCourseCollectionViewCell.h"
#import "YFArticlesCollectionViewCell.h"
#import "YFChatRoomModel.h"
#import "YFAlertsDetailViewController.h"
#import "YFArticleDetailViewController.h"
#import "YFSearchViewController.h"
#import "YFCourseModel.h"
#import "YFCourseOutlineViewController.h"
#import "YFNewCourseOneDetailViewController.h"
#import "AppDelegate.h"
#import "YFTeacherDetailViewController.h"

#import "YFJuHuaJRefreshGifHeader.h"
#import "YFSettingFontViewController.h"
#import "YFHomeThoughtsCollectionViewCell.h"
#import "YFCourseOneVideoCollectionViewCell.h"
#import <SJVideoPlayer/SJVideoPlayer.h>
#import "YFThoughModel.h"
#import "YFThingsDetailViewController.h"
#import "YFDetailVCRouteTool.h"

@interface YFHomeAllViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UIScrollViewDelegate,YFVideoCollectionViewCellDelegate,YFCourseOneVideoCollectionViewCellDelegate>

@property (nonatomic, strong)NSMutableArray *bannerArray;// banner
@property (nonatomic, strong)NSMutableArray *topCategoryArray;// 顶部4大分类
@property (nonatomic, strong)NSMutableArray *voice60sArray;// 语音60s
@property (nonatomic, strong)NSMutableArray *voice60sIdArray;// 语音60s

@property (nonatomic, strong)NSMutableArray *alertsArray;// 盘中资讯
@property (nonatomic, strong)NSMutableArray *coursesArray;// 课程
@property (nonatomic, strong)NSMutableArray *chatRoomsArray;// 直播
@property (nonatomic, strong)NSMutableArray *articlesArray;// 文章

@property (nonatomic, strong) UICollectionView *collectionView;

@property (nonatomic, assign) NSInteger currentIndex;
@property (nonatomic, assign) NSInteger currentIndexRow_showForward;

@property (nonatomic, strong, nullable) SJVideoPlayer *player;
@property (nonatomic, strong)NSMutableArray *dataArray;// 文章
@property (nonatomic, assign) NSInteger page;

@property (nonatomic, strong)NSMutableArray *adversArray;// banner
@property (nonatomic, strong)UIButton *moreBtn;

@end

@implementation YFHomeAllViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.currentIndex = -1;
    self.currentIndexRow_showForward = -1;
    self.page = 1;
    [self initFirstView];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playStatusChangeNoti:) name:YFPlayStatusChangeNotiName object:nil];
    
}

-(void)dealloc
{
    
}
-(void)kShieldingUserNoti:(NSNotification *)notification{
    NSString *userId = [NSString stringWithFormat:@"%@",notification.object[@"userId"]];
    NSString *has_follow = [NSString stringWithFormat:@"%@",notification.object[@"has_follow"]];
    NSString *type = [NSString stringWithFormat:@"%@",notification.object[@"type"]];
    if ([type isEqualToString:@"follow"]) {
        NSMutableArray *tempArray = [self.dataArray mutableCopy];
        for (int i = 0; i < tempArray.count; i++) {
            YFBaseListModel *base = [tempArray objectAtIndex:i];
            if ([base isKindOfClass:[YFBaseListModel class]]) {
                if ([base.user_id isEqualToString:userId]) {
                    //                    base.relates.has_following = [has_follow boolValue];
                    base.user.relates.has_following = [has_follow boolValue];
                }
            }
        }
        [self.collectionView reloadData];
        return;
    }
    
    NSMutableArray *tempArray = [self.dataArray mutableCopy];
    for (int i = 0; i < tempArray.count; i++) {
        YFBaseModel *base = [tempArray objectAtIndex:i];
        if ([base isKindOfClass:[YFBannerModel class]] == NO) {
            NSString *tempId = @"";
            switch (base.homeType) {//0 语音60S 1 专栏文章 2 动态 3 课程  4 快讯
                case 0:
                {
                    YFVoice60SModel *model = [tempArray objectAtIndex:i];
                    tempId = [NSString stringWithFormat:@"%@",model.user.Id];
                }
                    break;
                case 1:
                {
                    YFArticlesModel *model = [tempArray objectAtIndex:i];
                    tempId = [NSString stringWithFormat:@"%@",model.user.Id];
                }
                    break;
                case 2:
                {
                    YFThoughModel *model = [tempArray objectAtIndex:i];
                    tempId = [NSString stringWithFormat:@"%@",model.user_id];
                }
                    break;
                case 3:
                {
                    YFCourseModel *model = [tempArray objectAtIndex:i];
                    tempId = [NSString stringWithFormat:@"%@",model.user.Id];
                }
                    break;
                case 4:
                {
                    YFAlertsModel *model = [tempArray objectAtIndex:i];
                    tempId = [NSString stringWithFormat:@"%@",model.user.Id];
                }
                    break;
                default:
                    break;
            }
            if ([userId isEqualToString:tempId]) {
                [self.dataArray removeObject:base];
            }
        }
    }
    if (self.dataArray.count == 0) {
        [self loadMoreData];
    }else{
        [self.collectionView reloadData];
    }
}
-(void)switch_requestData
{
    //    if (self.dataArray.count == 0) {
    [self requestData];
    [self.collectionView setContentOffset:CGPointMake(0, 0) animated:NO];
    //    }
}
-(void)initFirstView{
    self.fd_prefersNavigationBarHidden = YES;
    // 主视图colleectionView
    [self setUpUI];
}
-(void)toTopClick:(UIButton *)sender{
    [self.collectionView setContentOffset:CGPointMake(0, 0) animated:YES];
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self updateVOice60sDataArray];
    [self hiddenTooView];
}
-(void)updateVOice60sDataArray{
    NSArray *voices = [self.voice60sArray copy];
    [self.voice60sArray  removeAllObjects];
    if ([ATVideoWindowView shareView].model.Id.length > 0 && voices.count == 0) {
        [ATVideoWindowView shareView].hidden = NO;
    }
    for (int i = 0; i < voices.count ;i++ ) {
        YFVoice60SModel *model = [voices safeObjectWithIndex:i];
        if ([model isKindOfClass:[YFVoice60SModel class]]) {
            if (([model.Id integerValue] == [[ATVideoWindowView shareView].model.Id integerValue]) && (model.isResolves == [ATVideoWindowView shareView].model.isResolves)) {
                model.playStatus = ATVideoWindowView.shareView.model.playStatus;
                self.currentIndex = i;
            }else{
                model.playStatus = 0;
            }
        }
        //        [self.voice60sArray safeAddObj:model];
        //        [self.voice60sIdArray safeAddObj:model.Id];
    }
    [self.collectionView reloadData];
}
-(void)setUpUI{
    self.collectionView = ({
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT -[YFUITool ajustBottomEdgeOnIphoneX:49]-[YFHomeTopCategoryCell getHomeTopCategoryCellHeight]) collectionViewLayout:layout];
        collectionView.backgroundColor = [UIColor whiteColor];
        collectionView.delegate = self;
        collectionView.dataSource = self;
        // 不满一屏幕也能滚动
        //        collectionView.alwaysBounceHorizontal = YES;
        
        [collectionView registerClass:[YFHomeBannerCell class] forCellWithReuseIdentifier:@"YFHomeBannerCell"];
        [collectionView registerClass:[YFHomeTopCategoryCell class] forCellWithReuseIdentifier:@"YFHomeTopCategoryCell"];
        [collectionView registerClass:[YFHomeThoughtsCollectionViewCell class] forCellWithReuseIdentifier:@"YFHomeThoughtsCollectionViewCell"];
        
        [collectionView registerClass:[YFNewVoice60sCollectionViewCell class] forCellWithReuseIdentifier:@"YFNewVoice60sCollectionViewCell"];
        [collectionView registerClass:[TYCyclePagerViewCell class] forCellWithReuseIdentifier:@"TYCyclePagerViewCell"];
        
        [collectionView registerClass:[YFIntradayAlertsCollectionViewCell class] forCellWithReuseIdentifier:@"YFIntradayAlertsCollectionViewCell"];
        [collectionView registerClass:[YFHomeCourseCollectionViewCell class] forCellWithReuseIdentifier:@"YFHomeCourseCollectionViewCell"];
        [collectionView registerClass:[YFHomeChatRoomsCollectionViewCell class] forCellWithReuseIdentifier:@"YFHomeChatRoomsCollectionViewCell"];
        [collectionView registerClass:[YFArticlesCollectionViewCell class] forCellWithReuseIdentifier:@"YFArticlesCollectionViewCell"];
        [collectionView registerClass:[YFCourseOneVideoCollectionViewCell class] forCellWithReuseIdentifier:@"YFCourseOneVideoCollectionViewCell"];
        
        [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"UICollectionViewCell"];
        collectionView.showsVerticalScrollIndicator = NO;
        collectionView.showsHorizontalScrollIndicator = NO;
        [self.view addSubview:collectionView];
        adjustsScrollViewInsets(collectionView);
        [collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.mas_equalTo(0);
            make.top.mas_equalTo(0);
        }];
        
        collectionView;
    });
    
    self.collectionView.mj_header = [YFJuHuaJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(requestData)];
}
-(void)requestData{
    self.page = 1;
    [self getData];
    [self getBanners];
    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    [delegate getUnreadMessageCount];
    
}
-(void)loadMoreData
{
    self.page ++;
    [self getData];
}
-(void)getBanners{
    return;
    if (self.userId.length > 0) {
        return;
    }
    NSString *urlString;
    urlString = kMakeupUrl(BASEURL, @"banners");
    NSMutableDictionary *param = [[NSMutableDictionary alloc]init];
    [param safeSetObj:@"0" forKey:@"type"];
    BOOL isCache = NO;
    if (self.bannerArray.count == 0) {
        isCache = YES;
    }
    [YFBaseNetWork yf_getNetworkWithParams:param urlHost:urlString isUseCache:isCache complete:^(NSDictionary *error, NSDictionary *result) {
        if (result) {
            [self.bannerArray removeAllObjects];
            if ([result isKindOfClass:[NSArray class]]) {
                NSArray *data = (NSArray *)result;
                for (int i = 0; i < data.count ;i++ ) {
                    NSDictionary *dict = [data safeObjectWithIndex:i];
                    YFBannerModel *banner = [YFBannerModel yy_modelWithDictionary:dict];
                    //                    [self.bannerArray addObject:banner];
                }
                [self.collectionView reloadData];
                //                [UIView performWithoutAnimation:^{
                //                    [self.collectionView reloadData];
                //                }];
            }
        }
    }];
}
-(void)getAdBanner{
    if ([self.is_better isEqualToString:@"1"]) {
        self.adversArray = [NSMutableArray array];
        [YFBaseNetWork yf_getNetworkWithParams:nil urlHost:kMakeupUrl(BASEURL, @"advers") isUseCache:NO complete:^(NSDictionary *error, NSDictionary *result) {
            if (result) {
                [self.adversArray removeAllObjects];
                if ([result isKindOfClass:[NSArray class]]) {
                    NSArray *data = (NSArray *)result;
                    for (int i = 0; i < data.count ;i++ ) {
                        NSDictionary *dict = [data safeObjectWithIndex:i];
                        YFBannerModel *banner = [YFBannerModel yy_modelWithDictionary:dict];
                        [self.adversArray addObject:banner];
                        if (self.dataArray.count > 0) {
                            if (self.dataArray.count > 5 *( i+1) ) {
                                [self.dataArray insertObject:banner atIndex:5 *( i+1)];
                            }
                        }
                    }
                    [self.collectionView reloadData];
                }
            }
        }];
    }
}
#pragma mark 网络请求
-(void)getData{
    NSString *urlString;
    NSMutableDictionary *param = [[NSMutableDictionary alloc]init];
    BOOL isCache = NO;
    if ([self.is_better isEqualToString:@"1"]) {
        urlString = kMakeupUrl(BASEURL, [NSString stringWithFormat:@"recommends"]);
    }else{
        if (self.userId.length > 0) {
            urlString = kMakeupUrl(BASEURL, [NSString stringWithFormat:@"users/%@/things",self.userId]);
        }else{
            urlString = kMakeupUrl(BASEURL, @"things");
            [param safeSetObj:self.is_better forKey:@"is_better"];
        }
        [param safeSetObj:@(20) forKey:@"per_page"];
        [param safeSetObj:@(self.page) forKey:@"page"];
        if (self.dataArray.count == 0) {
            isCache = YES;
        }
    }
    [param safeSetObj:@"user,relates" forKey:@"include"];
    
    [YFBaseNetWork yf_getNetworkWithParams:param urlHost:urlString isUseCache:isCache cancel:YES complete:^(NSDictionary *error, NSDictionary *result) {
        if (error) {
            
        }
        if (result) {
            if (self.page == 1) {
                [self.voice60sArray removeAllObjects];
                [self.voice60sIdArray removeAllObjects];
                [self.chatRoomsArray removeAllObjects];
                [self.coursesArray removeAllObjects];
                [self.articlesArray removeAllObjects];
                [self.alertsArray removeAllObjects];
                [self.dataArray removeAllObjects];
            }
            // banner
            NSArray *data = result[@"data"];
            if ([self.is_better isEqualToString:@"1"] && [result isKindOfClass:[NSArray class]]) {
                data = (NSArray *)result;
            }
            NSInteger adIndex= 0;
            for (int i = 0; i < data.count ;i++ ) {
                NSDictionary *dict = [data safeObjectWithIndex:i];
                NSInteger type = [[dict objectForKey:@"type"] integerValue];
                NSDictionary *modelD = [dict objectForKey:@"model"];
                if ([self.is_better isEqualToString:@"1"] ) {
                    modelD = [dict objectForKey:@"recommendable"];
                }
                if (self.adversArray.count > 0) {
                    if (i % 5 == 0 && i / 5 > 0) {
                        if (adIndex < self.adversArray.count) {
                            [self.dataArray addObject:[self.adversArray objectAtIndex:adIndex]];
                            adIndex++;
                        }
                    }
                }
                switch (type) {//0 语音60S 1 专栏文章 2 动态 3 课程  4 快讯
                    case 0:
                    {
                        YFVoice60SModel *model = [self getVoice:modelD withIndex:i];
                        model.homeType = type;
                        if (self.type == 1001) {
                            model.is_hot = [dict[@"is_lecturer_hot"] boolValue];
                            model.is_important = [dict[@"is_lecturer_important"] boolValue];
                        }
                        [self.dataArray addObject:model];
                        [self.dataArray addObject:model];
                        [self.dataArray addObject:model];
//                        [self.dataArray addObject:model];
//                        [self.dataArray addObject:model];

                    }
                        break;
                    case 1:
                    {
                        YFArticlesModel *model = [YFArticlesModel getArticlesModel:modelD];
                        model.homeType = type;
                        if (self.type == 1001) {
                            model.is_hot = [dict[@"is_lecturer_hot"] boolValue];
                            model.is_important = [dict[@"is_lecturer_important"] boolValue];
                        }
                        [self.dataArray addObject:model];
                    }
                        break;
                    case 2:
                    {
                        YFThoughModel *model = [self getThoughModel:modelD];
                        model.homeType = type;
                        [self.dataArray addObject:model];
                    }
                        break;
                    case 3:
                    {
                        YFCourseOneModel *model = [self getCourseOneModel:modelD];
                        model.homeType = type;
                        [self.dataArray addObject:model];
                    }
                        break;
                    case 4:
                    {
                        YFAlertsModel *model = [self getAlertModel:modelD];
                        model.homeType = type;
                        [self.dataArray addObject:model];
                    }
                        break;
                        
                    case 5:
                    {
                        NSDictionary *modelD = dict;
                        if (modelD[@"model"]  && [modelD[@"model"] isKindOfClass:[NSDictionary class]]) {
                            modelD = modelD[@"model"];
                        }
                        YFVoice60SModel *model = [YFVoice60SModel yy_modelWithDictionary:modelD];
                        model.title = [NSString stringWithFormat:@"【解读】%@",model.title];
                        model.cellHeight = [self getCellHeightWithString:model];
                        model.isResolves = 1;
                        model.homeType = 5;
                        [self.dataArray safeAddObj:model];
                    }
                        break;
                        
                    default:
                        break;
                }
            }
            NSDictionary *meta = [result objectForKey:@"meta"];
            NSDictionary *pagination = [meta objectForKey:@"pagination"];
            NSString *total = [NSString stringWithFormat:@"%@",[pagination objectForKey:@"total"]];
            if ((self.dataArray.count  - adIndex)< [total integerValue] && [total integerValue] > 0) {
                self.collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
            }else{
                self.collectionView.mj_footer  = nil;
            }
        }
        //        [UIView performWithoutAnimation:^{
        //            [self.collectionView reloadData];
        //        }];
        [self.collectionView reloadData];
        
        [self.collectionView.mj_header endRefreshing];
        [self.collectionView.mj_footer endRefreshing];
        if (self.adversArray.count == 0) {
            [self getAdBanner];
        }
    }];
}


-(CGFloat)getCellHeightWithString:(YFVoice60SModel *)model{
    CGFloat height = [model.title mt_heightWithWidth: WINDOW_WIDTH - 46 font:[UIFont zg_boldSystemFontWithSize:15] ];
    if (model.url == nil || model.url.length == 0) {
        CGFloat contentHeight = [model.text mt_heightWithWidth: WINDOW_WIDTH - 56 font:[UIFont zg_boldSystemFontWithSize:15] ] + 50;
        height += 132+[YFHomeCellBottomView yfGetHeight:model.user baseListModel:model] + contentHeight;
        if (model.target_location.length == 0 && model.tags.count == 0) {
            height -= 25;
        }
    }else{
        height += 210+[YFHomeCellBottomView yfGetHeight:model.user baseListModel:model];
        if (model.target_location.length == 0 && model.tags.count == 0) {
            height -= 25;
        }
    }
    height += 118;
    return height - 48;
}

-(YFCourseOneModel *)getCourseOneModel:(NSDictionary *)dict
{
    YFCourseOneModel *model = [YFCourseOneModel yy_modelWithDictionary:dict];
    YFThoughResourceModel *resModel = [[YFThoughResourceModel alloc]init];
    resModel.type = 1;
    resModel.url = model.video;
    resModel.preview_url = model.cover;
    model.resources = @[resModel];
    model.cellHeight = 118;
    CGFloat titleHeight = [model.title mt_heightWithWidth:WINDOW_WIDTH - 44 font:[UIFont zg_boldSystemFontWithSize:15] ];
    if (titleHeight > 24+20*2+ [UIFont zg_fontScalSize] * 2) {
        model.cellHeight += 24+20*2+ [UIFont zg_fontScalSize] * 2;
    }else{
        model.cellHeight += titleHeight;
    }
    //    [self.alertsArray safeAddObj:model];
    model.cellHeight += YFCourseOneVideoCollectionViewCell_oneImageHeight;// 视频
    model.cellHeight += [YFHomeCellBottomView yfGetHeight:model.user baseListModel:model] - 44;
    return model;
}
-(YFVoice60SModel *)getVoice:(NSDictionary *)dict withIndex:(NSInteger)index
{
    YFVoice60SModel *model = [YFVoice60SModel yy_modelWithDictionary:dict];
    CGFloat height = [model.title mt_heightWithWidth: WINDOW_WIDTH - 44 font:[UIFont zg_boldSystemFontWithSize:15] ];
    model.cellHeight += 180+height+[YFHomeCellBottomView yfGetHeight:model.user baseListModel:model];
    if (([model.Id integerValue] == [[ATVideoWindowView shareView].model.Id integerValue]) && (model.isResolves == [ATVideoWindowView shareView].model.isResolves)) {
        model.playStatus = ATVideoWindowView.shareView.model.playStatus;
        self.currentIndex = index;
        ATVideoWindowView.shareView.currentIndex = index;
        ATVideoWindowView.shareView.model = model;
    }
    if (model.target_location.length == 0 && model.tags.count == 0) {
        model.cellHeight -= 25;
    }
    [self.voice60sIdArray safeAddObj:model.Id];
    //    [self.voice60sArray safeAddObj:model];
    return model;
}
-(YFAlertsModel *)getAlertModel:(NSDictionary *)dict
{
    YFAlertsModel *model = [YFAlertsModel yy_modelWithDictionary:dict];
    model.cellHeight = 118;
    CGFloat titleHeight = [model.describe mt_heightWithWidth:WINDOW_WIDTH - 44 font:[UIFont zg_systemFontWithSize:15] ];
    if (titleHeight > 24+20*2+ [UIFont zg_fontScalSize] * 2) {
        model.cellHeight += 24+20*2+ [UIFont zg_fontScalSize] * 2;
    }else{
        model.cellHeight += titleHeight;
    }
    //    [self.alertsArray safeAddObj:model];
    return model;
}
-(YFThoughModel *)getThoughModel:(NSDictionary *)dict
{
    YFThoughModel *model = [YFThoughModel yy_modelWithDictionary:dict];
    model.cellHeight = 118 - 44;
    CGFloat titleHeight = [model.body mt_heightWithWidth:WINDOW_WIDTH - 44 font:[UIFont zg_boldSystemFontWithSize:15] ];
    if (titleHeight > 24+20*2+ [UIFont zg_fontScalSize] * 2) {
        model.cellHeight += 24+20*2+ [UIFont zg_fontScalSize] * 2;
    }else{
        model.cellHeight += titleHeight;
    }
    //    [self.alertsArray safeAddObj:model];
    if (model.resources.count > 0) {
        YFThoughResourceModel *resModel = model.resources.firstObject;
        if (resModel.type == 0) {
            model.cellHeight += YFHomeThoughtsCollectionViewCell_threeImageHeight;// 图片
        }else if (resModel.type == 1) {
            model.cellHeight += YFHomeThoughtsCollectionViewCell_oneImageHeight;// 视频
        }
    }
    model.cellHeight += [YFHomeCellBottomView yfGetHeight:model.user baseListModel:model];
    
    return model;
}

#pragma mark - CollectionView delegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    if (section == 0){
        if (self.bannerArray.count > 0) {
            return 1;
        }
        return 0;
    }
    //    else if (section == 1){
    //        return self.alertsArray.count;
    //    }else if (section == 2){
    //        return self.voice60sArray.count;
    //    }else if (section == 3){
    //        return self.articlesArray.count;
    //    }else if (section == 4){
    //        return self.alertsArray.count;
    //    }else if (section == 5){
    //        return self.coursesArray.count;
    //    }
    return self.dataArray.count ;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0){
        YFHomeBannerCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"YFHomeBannerCell" forIndexPath:indexPath];
        cell.dataArray = [self.bannerArray mutableCopy];
        return cell;
    }else{
        YFBaseModel *model = [self.dataArray objectAtIndex:indexPath.item];
        if ([model isKindOfClass:[YFBannerModel class]]) {
            TYCyclePagerViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TYCyclePagerViewCell" forIndexPath:indexPath];
            YFBannerModel *model = [self.dataArray safeObjectWithIndex:indexPath.row];
            [cell.imageV sd_setImageWithURL:[NSURL URLWithString:model.url] placeholderImage:SETIMAGE(@"logo")];
            return cell;
        }else  if ([model isKindOfClass:[YFVoice60SModel class]]){
            YFNewVoice60sCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"YFNewVoice60sCollectionViewCell" forIndexPath:indexPath];
            cell.backgroundColor = [UIColor whiteColor];
            
            cell.model = [self.dataArray safeObjectWithIndex:indexPath.row];
            if (indexPath.row == self.voice60sArray.count - 1) {
                //            cell.yfVoices60sView.yfBottomLineLabel.hidden = YES;
            }
            __block NSInteger row = indexPath.row;
            yfweakify(self);
            cell.yfBottomBtnClick = ^(YFModelChangeType type, YFVoice60SModel *model) {
                yfstrongify(self);
                if (type == YFModelChangeType_playAudio) {
                    [self hiddenTooView];
                    [self playVoice60s:indexPath.row];
                    //                    [self hiddenToolView];
                    //                    [self playVoice60sWithSection:indexPath.section row:indexPath.row];
                    return;
                }
                if (type != YFModelChangeType_showForward) {
                    [self hiddenTooView];
                }
                if (type == YFModelChangeType_showForward || type == YFModelChangeType_share|| type == YFModelChangeType_toText) {
                    
                    [self.voice60sArray replaceObjectAtIndex:row withObject:model];
                    
                    if (self.currentIndexRow_showForward > -1 && type == YFModelChangeType_showForward) {
                        if ( self.currentIndexRow_showForward == row) {
                            return ;
                        }
                        NSIndexPath *index_show = [NSIndexPath indexPathForRow:self.currentIndexRow_showForward inSection:2];
                        YFVoice60SModel *model1 = [self.voice60sArray safeObjectWithIndex:self.currentIndexRow_showForward];
                        model1.isShowForward = NO;
                        [self.voice60sArray replaceObjectAtIndex:self.currentIndexRow_showForward withObject:model1];
                        [self.collectionView reloadItemsAtIndexPaths:@[indexPath,index_show]];
                    }else{
                        [self.collectionView reloadItemsAtIndexPaths:@[indexPath]];
                    }
                    self.currentIndexRow_showForward = row;
                }else{
                    [self.collectionView reloadItemsAtIndexPaths:@[indexPath]];
                    if (indexPath.row == ATVideoWindowView.shareView.currentIndex) {
                        [ATVideoWindowView shareView].model = model;
                    }
                }
            };
            
            return cell;
            
        }else if ([model isKindOfClass:[YFArticlesModel class]]){
            YFArticlesCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"YFArticlesCollectionViewCell" forIndexPath:indexPath];
            cell.model = [self.dataArray safeObjectWithIndex:indexPath.row];
            //        if (indexPath.row == self.articlesArray.count - 1) {
            //            cell.yfBottonLabel.hidden = YES;
            //        }else{
            //            cell.yfBottonLabel.hidden = NO;
            //        }
            //        cell.yfHeadImageBtn.tag = indexPath.row;
            //        [cell.yfHeadImageBtn addTarget:self action:@selector(gotoUserDetailClick:) forControlEvents:UIControlEventTouchUpInside];
            return cell;
        }else if ([model isKindOfClass:[YFThoughModel class]]){
            YFHomeThoughtsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"YFHomeThoughtsCollectionViewCell" forIndexPath:indexPath ];
            cell.backgroundColor = [UIColor whiteColor];
            cell.model = [self.dataArray safeObjectWithIndex:indexPath.row];
            //        [cell requestData];
            cell.delegate = self;
            return cell;
        }else if ([model isKindOfClass:[YFCourseOneModel class]]){
            YFCourseOneVideoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"YFCourseOneVideoCollectionViewCell" forIndexPath:indexPath];
            cell.model = [self.dataArray safeObjectWithIndex:indexPath.row];
            cell.delegate = self;
            cell.yfHomeCellBottomView.middleBtn.tag = indexPath.row;
            [cell.yfHomeCellBottomView.middleBtn addTarget:self action:@selector(clickComments:) forControlEvents:UIControlEventTouchUpInside];
            cell.yfHomeCellBottomView.middleBtn.enabled = YES;
            return cell;
        }else if ([model isKindOfClass:[YFAlertsModel class]]){
            YFIntradayAlertsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"YFIntradayAlertsCollectionViewCell" forIndexPath:indexPath];
            //        if (indexPath.row == self.alertsArray.count - 1) {
            //            cell.yfBottomLabel.hidden = YES;
            //        }else{
            //            cell.yfBottomLabel.hidden = NO;
            //        }
            cell.model = [self.dataArray safeObjectWithIndex:indexPath.row];
            return cell;
        }
    }
    
    //    else if (indexPath.section == 5){
    //        YFHomeChatRoomsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"YFHomeChatRoomsCollectionViewCell" forIndexPath:indexPath];
    //        cell.dataArray = self.chatRoomsArray;
    //        return cell;
    //    }
    
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"UICollectionViewCell" forIndexPath:indexPath];
    return cell;
}

-(void)clickComments:(UIButton *)sender
{
    YFBaseModel *model = [self.dataArray safeObjectWithIndex:sender.tag];
    YFNewCourseOneDetailViewController *vc = [YFNewCourseOneDetailViewController new];
    YFCourseOneModel *courseOneModel = (YFCourseOneModel *)model;
    vc.model = courseOneModel;
    [vc performSelector:@selector(switch_requestData) withObject:nil afterDelay:0.2];
    [self.navigationController pushViewController:vc animated:YES];
}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    NSLog(@"%s",__func__);
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSLog(@"%s",__func__);
//    if ([self.is_better integerValue] == 1) {
//        [self.collectionView reloadData];
//    }
}
-(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
    NSLog(@"%s",__func__);
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [self hiddenTooView];
    if (_player.isPlaying == YES) {
        [_player pause];
    }
    if ([self.is_better integerValue] == 1) {
//        if ((scrollView.contentSize.height - scrollView.contentOffset.y < scrollView.frame.size.height ) && (scrollView.contentOffset.y > 0)) {
//            CGFloat offsety = scrollView.contentSize.height - scrollView.frame.size.height ;
//            if (offsety < 0) {
//                offsety = 0;
//            }
//            [scrollView setContentOffset:CGPointMake(scrollView.contentOffset.x, offsety) animated:NO];
//            [self.collectionView reloadData];
//            return;
//        }
        if (self.yfScrollOffset) {
//            if (scrollView.contentOffset.y <= 30) {
                self.yfScrollOffset(scrollView.contentOffset.y);
//            }else{
//
//            }
        }
    }else{
        if (self.yfScrollOffset) {
            self.yfScrollOffset(scrollView.contentOffset.y);
        }
    }
    NSLog(@"%lf",scrollView.contentOffset.y);
    
}

-(void)hiddenTooView
{
    if (self.currentIndexRow_showForward < 0) {
        return;
    }
    YFVoice60SModel *model1 = [self.dataArray safeObjectWithIndex:self.currentIndexRow_showForward];
    if (model1.isShowForward == NO) {
        return;
    }
    NSIndexPath *index_show = [NSIndexPath indexPathForRow:self.currentIndexRow_showForward inSection:2];
    model1.isShowForward = NO;
    [self.dataArray replaceObjectAtIndex:self.currentIndexRow_showForward withObject:model1];
    [self.collectionView reloadData];
}
-(void)gotoUserDetailClick:(UIButton *)sender{
    YFArticlesModel *model = [self.articlesArray safeObjectWithIndex:sender.tag];
    YFTeacherDetailViewController *vc = [YFTeacherDetailViewController new];
    vc.userModel = model.user;
    [self.navigationController pushViewController:vc animated:YES];
}
-(void)gotoCourseOneDetailClick:(UIButton *)sender{
    YFNewCourseOneDetailViewController *vc = [YFNewCourseOneDetailViewController new];
    YFCourseModel *model = [self.coursesArray safeObjectWithIndex:sender.tag];
    vc.model = model.latest_section;
    [self.navigationController pushViewController:vc animated:YES];
}
-(void)playVoice60s:(NSInteger )indexPathRow{
    
    [UIView animateWithDuration:0.15 animations:^{
        [ATVideoWindowView shareView].frame = CGRectMake(10, WINDOW_HEIGHT-[YFUITool ajustBottomEdgeOnIphoneX:110+[UIFont zg_fontScalSize]*2], WINDOW_WIDTH - 20, 50+[UIFont zg_fontScalSize]*2);
    }];
    [ATVideoWindowView shareView].isPlayNormalVoice60s = YES;
    [ATVideoWindowView shareView].allVoice60sModelArray = [self.voice60sArray copy];
    
    YFVoice60SModel *model = [self.dataArray safeObjectWithIndex:indexPathRow];
    [ATVideoWindowView shareView].currentIndex = indexPathRow;
    [[ATVideoWindowView shareView] voice60sHiddenMore];
    [ATVideoWindowView showViewPlayWithModel:model];
    
    self.currentIndex = indexPathRow;
}
-(void)playStatusChangeNoti:(NSNotification *)notification{
    NSMutableArray *vcs = [self.navigationController.viewControllers mutableCopy];
    [vcs removeLastObject];
    UIViewController *vc = [vcs lastObject];
    BOOL isCurrentVC = NO;
    if ((vc == self)&&[vc isKindOfClass:[YFBaseViewController class]] == YES ) {
        isCurrentVC = YES;
    }else{
        isCurrentVC = NO;
    }
    if (self.currentIndex > -1 ) {
        YFModelChangeType type = [notification.userInfo[@"type"] integerValue];
        YFVoice60SModel *model = notification.userInfo[@"model"];
        if ([self.voice60sIdArray containsObject:model.Id] == NO) {
            return;
        }
        ATVideoWindowView.shareView.currentIndex = [self.voice60sIdArray indexOfObject:model.Id];
        if (model) {
            if (type == YFModelChangeType_finish) {
                model.playStatus = 0;
            }
            if (type == YFModelChangeType_nextOrShang) {
                self.currentIndex = ATVideoWindowView.shareView.currentIndex;
            }
            [self.dataArray replaceObjectAtIndex:self.currentIndex withObject:model];
        }
        //        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.currentIndex inSection:2];
        [self.collectionView reloadData];
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0 && self.bannerArray.count >0){
        //        return CGSizeMake(WINDOW_WIDTH,YFHomeBannerCell_height);
        return CGSizeMake(WINDOW_WIDTH,0.1);
    }
    YFBaseModel *base = [self.dataArray objectAtIndex:indexPath.item];
    if ([base isKindOfClass:[YFBannerModel class]]) {
        return CGSizeMake(WINDOW_WIDTH, 100);
    }
    CGFloat cellHeight = 0.0;
    switch (base.homeType) {//0 语音60S 1 专栏文章 2 动态 3 课程  4 快讯
        case 0:
        case 5:
        {
            YFVoice60SModel *model = [self.dataArray objectAtIndex:indexPath.item];
            cellHeight = model.cellHeight;
        }
            break;
        case 1:
        {
            YFArticlesModel *model = [self.dataArray objectAtIndex:indexPath.item];
            cellHeight = model.cellHeight;
        }
            break;
        case 2:
        {
            YFThoughModel *model = [self.dataArray objectAtIndex:indexPath.item];
            cellHeight = model.cellHeight;
        }
            break;
        case 3:
        {
            YFCourseModel *model = [self.dataArray objectAtIndex:indexPath.item];
            cellHeight = model.cellHeight;
        }
            break;
        case 4:
        {
            YFAlertsModel *model = [self.dataArray objectAtIndex:indexPath.item];
            cellHeight = model.cellHeight;
        }
            break;
            
        default:
            break;
    }
    return CGSizeMake(WINDOW_WIDTH, cellHeight);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    YFBaseModel *base = [self.dataArray objectAtIndex:indexPath.item];
    if ([base isKindOfClass:[YFBannerModel class]]) {
        YFBannerModel *model;
        model = [self.dataArray safeObjectWithIndex:indexPath.row];
        [model clickWithVC:self];
        return;
    }
    //////0 语音60S 1 专栏文章 2 动态 3 课程  4 快讯
    switch (base.homeType) {
        case 0://
        {
            //            [self hiddenTooView];
            //            [self playVoice60s:indexPath.row];
        }
            break;
        case 2://
        {
            YFThoughModel *model;
            model = [self.dataArray safeObjectWithIndex:indexPath.row];
            YFThingsDetailViewController *vc = [YFThingsDetailViewController new];
            vc.model = model;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 4:// 要闻解读
        {
            YFAlertsModel *model = [self.dataArray safeObjectWithIndex:indexPath.row];
            YFAlertsDetailViewController *vc = [YFAlertsDetailViewController new];
            vc.model = model;
            vc.yfUpdateModelBlock = ^(YFBaseListModel * _Nonnull backModel) {
                backModel.cellHeight = model.cellHeight;
                [self.dataArray replaceObjectAtIndex:indexPath.row withObject:backModel];
                [self.collectionView reloadData];
            };
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 3:// 课程
        {
            YFNewCourseOneDetailViewController *vc = [YFNewCourseOneDetailViewController new];
            YFCourseOneModel *courseOneModel = (YFCourseOneModel *)base;
            vc.model = courseOneModel;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 1:// 专栏
        {
            YFArticlesModel *model;
            model = [self.dataArray safeObjectWithIndex:indexPath.row];
            YFArticleDetailViewController *vc = [YFArticleDetailViewController new];
            vc.model = model;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 5:{
            YFVoice60SModel *tempModel = [self.dataArray safeObjectWithIndex:indexPath.row];
            [YFDetailVCRouteTool yfJumpToVCWithType:YFRouteType_Alerts Id:tempModel.alert_id currentVC:self];
        }
            break;
        default:
            break;
    }
}

- (void)coverItemWasTapped:(UICollectionViewCell *)cell {
    NSIndexPath *indexPath = [self.collectionView indexPathForCell:cell];
    
    if ( !_player ) {
        _player = [SJVideoPlayer player];
    }
    YFThoughModel *model = [self.dataArray objectAtIndex:indexPath.item];
    YFThoughResourceModel *resModel = model.resources.firstObject;
    NSString *url = [NSString stringWithFormat:@"%@",resModel.url];
    NSString *title = [NSString stringWithFormat:@"%@",model.body];
    _player.URLAsset = [[SJVideoPlayerURLAsset alloc] initWithURL:[NSURL URLWithString:url?:@""] playModel:[SJPlayModel playModelWithCollectionView:self.collectionView indexPath:indexPath]];
    _player.URLAsset.title = title;
}
-(void)courseOneCoverItemWasTapped:(UICollectionViewCell *)cell
{
    NSIndexPath *indexPath = [self.collectionView indexPathForCell:cell];
    
    if ( !_player ) {
        _player = [SJVideoPlayer player];
    }
    YFCourseOneModel *model = [self.dataArray objectAtIndex:indexPath.item];
    YFThoughResourceModel *resModel = model.resources.firstObject;
    NSString *url = [NSString stringWithFormat:@"%@",resModel.url];
    NSString *title = [NSString stringWithFormat:@"%@",model.title];
    _player.URLAsset = [[SJVideoPlayerURLAsset alloc] initWithURL:[NSURL URLWithString:url?:@""] playModel:[SJPlayModel playModelWithCollectionView:self.collectionView indexPath:indexPath]];
    _player.URLAsset.title = title;
}

-(void)pusToWebWithUrl:(NSString *)url title:(NSString *)title{
    //web
    YFBaseWebViewController *webVc = [[YFBaseWebViewController alloc]init];
    [webVc setUrlStr:url?:@""];
    webVc.title = title;
    [self.navigationController pushViewController:webVc animated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark lazy load

-(NSMutableArray *)bannerArray{
    if (_bannerArray == nil) {
        _bannerArray = [NSMutableArray arrayWithCapacity:5];
    }
    return _bannerArray;
}
-(NSMutableArray *)alertsArray{
    if (_alertsArray == nil) {
        _alertsArray = [NSMutableArray arrayWithCapacity:5];
    }
    return _alertsArray;
}
-(NSMutableArray *)voice60sArray{
    if (_voice60sArray == nil) {
        _voice60sArray = [NSMutableArray arrayWithCapacity:5];
    }
    return _voice60sArray;
}
-(NSMutableArray *)coursesArray{
    if (_coursesArray == nil) {
        _coursesArray = [NSMutableArray arrayWithCapacity:5];
    }
    return _coursesArray;
}
-(NSMutableArray *)chatRoomsArray{
    if (_chatRoomsArray == nil) {
        _chatRoomsArray = [NSMutableArray arrayWithCapacity:5];
    }
    return _chatRoomsArray;
}
-(NSMutableArray *)articlesArray{
    if (_articlesArray == nil) {
        _articlesArray = [NSMutableArray arrayWithCapacity:5];
    }
    return _articlesArray;
}
-(NSMutableArray *)voice60sIdArray{
    if (_voice60sIdArray == nil) {
        _voice60sIdArray = [NSMutableArray arrayWithCapacity:5];
    }
    return _voice60sIdArray;
}
-(NSMutableArray *)dataArray
{
    if (_dataArray == nil) {
        _dataArray = [[NSMutableArray alloc]init];
    }
    return _dataArray;
}

@end
